<?php
namespace app\index\controller;

use think\Controller;

use app\index\model\IndexModel;
use think\Db;

use think\Session;
class Border extends Controller
{
    public $list = null;
    public $res = null;
    public $banner = null;
    public $style = null;
    public $sort = null;
    public $data1 = null;



    public function _initialize()
    {


        $a = Db::field('m.id id,u.username username,u.id uid,photo,content,time,collectnum,commentnum,transpondnum,agreenum,readnum,hot,src_id')
            ->table(['wb_message' => 'm', 'wb_buser' => 'u','wb_buserinfo'=>'i'])
            ->where('m.uid = u.id and m.hot = 2 and u.id = i.uid')
            ->order(['id' => 'desc'])
            ->select();

        foreach ($a as $k => $v) {
            $path = Db::field('id pid,path')
                ->table(['wb_mpicture'])
                ->where('mid=' . $v['id'])
                ->order(['id' => 'asc'])
                ->select();
            $a[$k]['path'] = $path;
            if (!empty($v['src_id'])){
//                halt($v['src_id']);
                $res = Db::field('m.id id,u.username username,u.id uid,photo,content,time')
                    ->table(['wb_message' => 'm', 'wb_buser' => 'u','wb_buserinfo'=>'i'])
                    ->where('m.uid = u.id and  i.uid = u.id  and  m.id = '.$v['src_id'])
                    ->find();
//                halt($res);
                $a[$k]['old'] = $res;
                $path = Db::field('path')
                    ->table(['wb_mpicture'=>'mp'])
                    ->where('mp.mid = '.$v['src_id'])
                    ->find();
//                halt($path);
                if (!empty($path)){
                    $a[$k]['old']['path'] = $path['path'];
                }
            }

        }
        $this->list = $a;

        $this->res = Db::field('id,name')
            ->table(['wb_talk'])
            ->where('hot=2')
            ->order(['id' => 'asc'])
            ->select();
//        halt($res);


        $this->banner = Db::field('id,path,title,link')
            ->table(['wb_banner'])
            ->order(['id' => 'desc'])
            ->select();
        $this->style = Db::field('style,id')
            ->table(['wb_style'])
            ->order(['id' => 'asc'])
            ->select();
        $this->sort = Db::field('style,id')
            ->table(['wb_style'])
            ->order(['id' => 'asc'])
            ->select();
        $apikey = 'd4042adba92f950b2cf89701699ecff4';
        $url = 'http://v.juhe.cn/toutiao/index?type=top&key=' . $apikey;
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        $data = curl_exec($curl);
        curl_close($curl);
        $jsonObj = json_decode($data, true);
        $result = $jsonObj["result"];
        $this->data1= $result["data"];

//        halt($list);


    }
}